package com.vehicle_demo.controller;

import com.vehicle_demo.entity.VehicleInfo;
import com.vehicle_demo.common.ResponseStatus;
import com.vehicle_demo.service.VehicleInfoService;
import com.vehicle_demo.utils.Vid_Auto;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/vehicles")
public class VehicleController {

        @Autowired
        private VehicleInfoService vehicleInfoService;

        @GetMapping("/all")
        public List<VehicleInfo> findAll() {
                return vehicleInfoService.findAll();
        }



        @PostMapping("/{id}")
        public List<VehicleInfo> findById(@PathVariable Integer id) {
                List<VehicleInfo> byIdInfo = vehicleInfoService.findById(id);
                if (byIdInfo.isEmpty()) {
                        return new ArrayList<>();
                }
                return byIdInfo;
        }


        @PostMapping("/insert")
        public ResponseEntity<String> insertVehicleInfo(@RequestBody VehicleInfo vehicleInfo) {
                String vid = Vid_Auto.generateRandomString();
                vehicleInfo.setVid(vid);
                ResponseStatus result = vehicleInfoService.insert(vehicleInfo);
                return new ResponseEntity<>(result.getMessage(), HttpStatus.OK);
        }

        @PostMapping("/update")
        public ResponseEntity<String> updateVehicleInfo(@Valid @RequestBody VehicleInfo vehicleInfo) {
                ResponseStatus result = vehicleInfoService.update(vehicleInfo);
                return new ResponseEntity<>(result.getMessage(), HttpStatus.OK);
        }

        @PostMapping("/delete")
        public ResponseEntity<String> deleteVehicleInfo(@Valid @RequestBody int id) {
                ResponseStatus result = vehicleInfoService.delete(id);
                return new ResponseEntity<>(result.getMessage(), HttpStatus.OK);
        }


}

